package com.lbm.business.web.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import com.lbm.baseclass.controller.LBMBaseController;
import com.lbm.baseclass.utils.json.JsonUtil;
import com.lbm.business.service.IModuleInfoService;
import com.lbm.model.ModuleInfo;
import com.lbm.model.RoleInfo;
import com.lbm.model.RoleModule;

public class ModuleInfoController extends LBMBaseController{
	private IModuleInfoService moduleInfoService;

	public void setModuleInfoService(IModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}
	/**
	* 模块列表
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleinfo/moduleinfolist.jsp");
	    return mv;
	}
	/**
	* 加载模块列表数据
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView ajaxlist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String hql="from ModuleInfo where 1=1";
		String sort=request.getParameter("sort");
		String order=request.getParameter("order");
		if(sort!=""&&sort!=null)hql+=" order by "+sort+" "+order;
		else hql+=" order by moduleNo";
		List<ModuleInfo> list=calcPage(request, moduleInfoService, hql);
		int total=moduleInfoService.getAllModuleInfo().size();
		String json="";
		json = "{\"total\": " + total + ", \"rows\": "
				+ JsonUtil.toString(list, JsonUtil.COLLECTION_FILTER) + "}";
		renderJson(response, json);
	    return null;
	}
	/**
	* 添加
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleinfo/moduleinfoform.jsp");
		return mv;
	}
	/**
	* 修改
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView modify(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv=new ModelAndView("WEB-INF/jsp/moduleinfo/moduleinfoform.jsp");
		ModuleInfo moduleInfo=moduleInfoService.getModuleInfoById(request.getParameter("id"));
		mv.addObject("moduleInfo", moduleInfo);
		return mv;
	}
	/**
	* 删除
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String id=request.getParameter("ids");
		String[] ids=id.split(",");
		for(int i=0;i<ids.length;i++){
			try {
				ModuleInfo module = moduleInfoService.getModuleInfoById(ids[i]);
				if (module != null)
					moduleInfoService.deleteModuleInfo(ids[i]);
			} catch (Exception e) {
				log.error(e);
				flag=false;
			}
		}
		renderJson(response, toJson("success", flag));
		return null;
	}
	/**
	* 保存
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		boolean flag=true;
		String id=request.getParameter("moduleid");
		String name=request.getParameter("moduleName");
		String code=request.getParameter("moduleCode");
		String fname=request.getParameter("parentModuleid");
		String addr=request.getParameter("addr");
		String moduleIcon=request.getParameter("moduleIcon");
		String moduleNo=request.getParameter("moduleNo");
		ModuleInfo moduleInfo=new ModuleInfo();
		moduleInfo.setModuleid(id);
		moduleInfo.setModuleName(name);
		moduleInfo.setModuleIcon(moduleIcon);
		moduleInfo.setParentModuleid(fname);
		moduleInfo.setAddr(addr);
		moduleInfo.setModuleCode(code);
		moduleInfo.setModuleNo(moduleNo);
		try {
			moduleInfoService.saveOrUpdateModuleInfo(moduleInfo);
		} catch (Exception e) {
			log.error(e);
			flag=false;
		}
		List<RoleInfo> ui=baseService.getValueObjectsByHQL("from RoleInfo where roleName='超级管理员'");
		if(ui.size()!=0){
			RoleModule rm=new RoleModule();
			rm.setRoleid(ui.get(0).getRoleid());
			rm.setModuleid(moduleInfo.getModuleid());
			baseService.saveOrUpdateVO(rm);
		}
		renderJson(response, toJson("success",flag));
		return null;
	}
	/**
	* 检查模块名是否已存在
	* @param request
	* @param response
	* @return
	* @throws Exception
	*/
	public void check(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		boolean flag=true;
		if(moduleInfoService.getModuleInfoByHQL("from ModuleInfo where moduleName='"+java.net.URLDecoder.decode(request.getParameter("moduleName"), "UTF-8")+"'").size()!=0)flag=false;
		renderJson(response,toJson("success",flag));
	}
}
